﻿using Api.Core.Common.Helper;
using Api.Core.IServices.User;
using Api.Core.Model;
using Api.Core.Model.User;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Api.Core.Controllers
{

    public class UserController : BaseController
    {
        private readonly IUserInfoService _userInfoService;
        private readonly IMapper _mapper;

        public UserController(IUserInfoService userInfoService
            , IMapper mapper)
        {
            _userInfoService = userInfoService;
            _mapper = mapper;
        }


        [HttpGet("test")]
        public IActionResult test2()
        {
            return Ok("123");
        }


        /// <summary>
        /// qq
        /// </summary>
        /// <param name="userDto"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("user")]
        public async Task<PageModel<UserDto>> Get(UserDto userDto = null)
        {

            var userModel = await _userInfoService.GetUser(userDto);
            return userModel;
        }

        [HttpGet("userId")]
        public async Task<UserDto> GetById(string Id)
        {
            var userModel = await _userInfoService.Query(x => x.UserId == Id);
            var user = _mapper.Map<UserEntity, UserDto>(userModel.FirstOrDefault());
            return user;
        }

        [HttpPost("userState")]
        public async Task<IActionResult> ChangeUserState(UserDto userDto)
        {
            var info = await _userInfoService.Query(x => x.UserId == userDto.UserId);
            var user = info.FirstOrDefault();
            user.IsDel = userDto.IsDel;
            var isTrue = await _userInfoService.Update(user);
            return new JsonResult(new
            {
                sussess = isTrue
            });
        }

        [HttpPost("updateUser")]
        public async Task<IActionResult> UpdateUser(UserDto userDto)
        {
            var info = await _userInfoService.Query(x => x.UserId == userDto.UserId);
            var user = info.FirstOrDefault();
            _mapper.Map(userDto, user);
            var isTrue = await _userInfoService.Update(user);
            return new JsonResult(new
            {
                sussess = isTrue
            });
        }


        [HttpPost("CreatUser")]
        public async Task<IActionResult> AddUser(UserDto userDto)
        {
            var user = _mapper.Map<UserDto, UserEntity>(userDto);
            user.Password = MD5Helper.MD5Encrypt32(user.Password);
            user.UserId = Guid.NewGuid().ToString();
            var isTrue = await _userInfoService.Add(user);
            return new JsonResult(new
            {
                sussess = isTrue == 0 ? true : false
            });
        }


        [HttpPost("DelUser")]
        public async Task<IActionResult> DelUser(UserDto userDto)
        {
            var isTrue = await _userInfoService.DeleteById(userDto.UserId);
            return new JsonResult(new
            {
                sussess = isTrue
            });
        }


        #region test hangfire with token
        //[HttpGet]
        //[AllowAnonymous]
        //[Route("qwe")]
        //public void TestAddRecurringJob()
        //{
        //    Dictionary<string, string> Headers = new Dictionary<string, string>();
        //    Headers.Add("authorization", "Bearer eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoibXVzaHVpIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZWlkZW50aWZpZXIiOiJCQ0U3RDc1RS05M0I5LTQ2QzAtODhEMi1BRjlDM0M4OEFBNEUiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2V4cGlyYXRpb24iOiIyMDIwLzQvNSAxMzoyNzoyMyIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9jbGFpbXMvcm9sZSI6Iui2hee6p-euoeeQhuWRmCIsIm5iZiI6MTU4NjA2MjY0MywiZXhwIjoxNTg2MDY0NDQzLCJpc3MiOiJBcGlDb3JlIiwiYXVkIjoiaHR0cDovL2xvY2FsaG9zdDo1MDAwLyJ9.2_OUorV1ufuNz7GSiWVMttgomgJ10ar_FIIryWkkVYU");
        //    var serverUrl = "http://localhost:5002/job";
        //    var result = HangfireJobClient.AddBackgroundJob(serverUrl, new BackgroundJob()
        //    {
        //        JobName = "测试5点40执行",
        //        Method = "Get",
        //        Data = "",
        //        Url = "https://localhost:5001/api/User/test",
        //        Mail = new List<string> { "1073376250@qq.com" },
        //        SendSucMail = false,
        //       // Cron = "* * * * *",
        //        Headers = Headers

        //    }, new HangfireServerPostOption
        //    {
        //        BasicUserName = "admin",
        //        BasicPassword = "test"
        //    });
        //}
        #endregion
    }
}